package adapter;

import java.util.Date;

import com.necsv.Mylog.Mylog;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ToDoDBAdapter {
	private static final String TAG = "ToDoDBAdapter";
	private static final String DATABASE_NAME = "todoList3.db";
	private static final int VERSION = 1;
	private static final String DATABASE_TABLE = "sonnt";
	public static final String KEY_ID = "_id";
	public static final String KEY_TASK = "task";
	public static final String KEY_CREATION_DATE = "creation_date";
	Context context;
	SQLiteDatabase db;
	toDoDBOpenHelper dbHelper;
	public ToDoDBAdapter(Context context)
	{
		this.context = context;
		dbHelper = new toDoDBOpenHelper(context, DATABASE_NAME, null, VERSION);
	}
	public void open(){
		try {
			db = dbHelper.getWritableDatabase();
		} catch (Exception e) {
			db = dbHelper.getReadableDatabase();
			Mylog.Toat(context, "Only read");
			// TODO: handle exception
		}
	}
	public void close(){
		db.close();
	}
	
	public Cursor getAllTodo(){
		return db.query(DATABASE_TABLE, new String[]{KEY_ID, KEY_TASK,KEY_CREATION_DATE}, null, null, null, null, null);
	}
	
	public Cursor getCursorTodo(long id)
	{
		Cursor mCursor = null;
		try {
			String wherer = KEY_ID + "=" + id;
			mCursor = db.query(true, DATABASE_TABLE, new String[]{KEY_ID,KEY_TASK}, wherer, null, null, null, null, null);
		} catch (Exception e) {
			// TODO: handle exception
			Log.w(TAG, "err get Cursor: " + e.toString());
		}
		return mCursor;
	}
	
	public ToDoItem getTodoItem(long id)
	{
		ToDoItem result = new ToDoItem();
		Cursor mCursor = null;
		try {
			String wherer = KEY_ID + "=" + id;
			mCursor = db.query(true, DATABASE_TABLE, new String[]{KEY_ID,KEY_TASK}, wherer, null, null, null, null, null);	
			if(mCursor.getCount() == 0 || !mCursor.moveToFirst())
				throw new SQLException("No item id row : " + id);
			
			result = new ToDoItem(mCursor.getString(1),new Date());
			} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "err todo get item" + e.toString());
		}
			return result;
	}
	//insert new task
	public long insert(ToDoItem task){
		Log.v(TAG, "insert: " + task.getTask());
		ContentValues cv = new ContentValues();
		cv.put(KEY_TASK, task.getTask());
		cv.put(KEY_CREATION_DATE, task.getCreate().getTime());
		return db.insert(DATABASE_TABLE, null, cv);
	}
	public Boolean remove(long id){
		
		String where = KEY_ID + "=" + id;
		return db.delete(DATABASE_TABLE, where, null) > 0;
	}
	 
	public Boolean update(long id, String task){
		String where = KEY_ID + "=" + id ;
		ContentValues cv = new ContentValues();
		cv.put(KEY_TASK, task);
		return db.update(DATABASE_TABLE, cv, where, null) > 0;
	}
	
	
	private static class toDoDBOpenHelper extends SQLiteOpenHelper{

		private static final String DATABASE_CREATE = "CREATE TABLE " + DATABASE_TABLE + 
			"(" +
				KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
				KEY_TASK + " TEXT NOT NULL," +
				KEY_CREATION_DATE + " LONG" +
			");";

		public toDoDBOpenHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			Log.v(TAG, "contructor db");
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.w(TAG, "update version:" + oldVersion + " to version :" + newVersion 
					+ ", destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
		
	}

}
